package cn.edu.zut.dao;

import cn.edu.zut.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
@Repository
public class StudentDaoImpl implements StudentDao{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    Student student;
    @Autowired
    List<Student> students;
    @Override
    public void insertStudent(Student student) {
        String INSERT_STUDENT="INSERT INTO student(number,name,sex,grade) VALUES(?,?,?,?)";
        Object[] args={student.getNumber(),student.getName(),student.getSex(),student.getGrade()};
        jdbcTemplate.update(INSERT_STUDENT,args);
    }

    @Override
    public void deleteStudent(Student student) {
        String DETELE_STUDENT="DELETE FROM student where number=?";
        jdbcTemplate.update(DETELE_STUDENT,student.getNumber());
    }

    @Override
    public List<Student> queryAll() {
        String QUERY_ALL_STUDENT="SELECT * FROM STUDENT";
        RowMapper<Student> rowMapper=new BeanPropertyRowMapper<>(Student.class);
        students=jdbcTemplate.query(QUERY_ALL_STUDENT,rowMapper);
        return students;
    }
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Resource(name = "list")
    public void setStudents(List<Student> students) {
        this.students = students;
    }
}
